/*
2022-3-6
https://leetcode-cn.com/problems/append-k-integers-with-minimal-sum/
*/ 
class Solution {
public:
    long long minimalKSum(vector<int>& nums, int k) {
        long long ans=0,sum=0;
        set<int> S;
        for(auto c:nums) S.insert(c);
        long long n=k;
        for(auto c:S)
        {
            if(c<=n)
            {
                sum+=c;
                n++;
            }
        }
        ans=(n+1)*n/2;
        return ans-sum;
    }
};
